// ignore_for_file: file_names, must_be_immutable, constant_identifier_names, slash_for_doc_comments, avoid_unnecessary_containers, prefer_const_constructors, unnecessary_cast, sized_box_for_whitespace, prefer_function_declarations_over_variables, overridden_fields, annotate_overrides, prefer_const_literals_to_create_immutables, unnecessary_null_comparison, deprecated_member_use

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:mybase/base/tool/debug/DebugHelp.dart';
import 'package:mybase/base/tool/file/FFPath.dart';
import 'package:mybase/base/tool/file/FFile.dart';
import 'package:mybase/base/tool/pprint/FFLog.dart';
import 'package:mybase/base/tool/time/FFTime.dart';
import 'package:mybase/base_new/help/JJEmpty.dart';

Future<void> log_write({
  required Object isa,
  required String key,
  required Object value,
}) async {
  log_info("${isa.toString()}:: $key >>> ${value.toString()}");
  await WriteDocumentHelp().write(key: key, value: value.toString());
}

/// 将内容写入到沙盒
class WriteDocumentHelp {
  // 单例公开访问点
  factory WriteDocumentHelp() => _sharedInstance();

  // 静态、同步、私有访问点
  static WriteDocumentHelp _sharedInstance() {
    return _instance;
  }

  // 静态私有成员，没有初始化
  static final WriteDocumentHelp _instance = WriteDocumentHelp._();

  // 私有构造函数
  WriteDocumentHelp._() {
    DateTime? formatTime = FFTime.strDateToDate(maxDate);
    if (formatTime == null) {
      _enable = false;
      return;
    }

    DateTime nowTime = FFTime.now();
    int nowSecond = FFTime.dateToTimestampSecond(nowTime);
    int formatTimeSecond = FFTime.dateToTimestampSecond(formatTime);

    if (formatTimeSecond < nowSecond) {
      _enable = false;
      return;
    }

    _enable = true;
  }

  bool _enable = false;

  String filePath = "";

  /// 启动时间超过这个时间不打印
  String maxDate = "2025-06-06 19:00:00";

  /// 写入文本
  Future<void> write({
    required String key,
    required String value,
  }) async {
    if (!_enable) return;

    if (strIsEmpty(filePath)) {
      String document = await FFPath.getPhoneLocalPath();
      log_info("沙盒目录 document目录 ${document}");
      String time = FFTime.dateFormat(FFTime.now());
      filePath = document + "/runlog/" + time + ".txt";
    }

    await FFile.writeContentByPath(
      filePath: filePath,
      content: "${FFTime.dateFormat(FFTime.now())}  $key >>> $value\n",
      autoCreate: true,
    );
  }

}
